package com.example.demo.util.codeGen;

import com.example.demo.util.StrUtil;
import com.example.demo.util.StringUtils;
//import com.gm.wj.entity.ColumnInfo;
//import com.gm.wj.util.StrUtil;
import lombok.Data;

import java.util.List;

@Data
public class TableInfo {
    String tableName;
    String 类名;
    List<ColumnInfo> columnInfos;
    String 实体名;

    public TableInfo() {
    }

    public TableInfo(String tableName, List<ColumnInfo> columnInfos) {
        this.tableName = tableName;
        this.columnInfos = columnInfos;
        实体名=   StringUtils.underlineToCamelCase(tableName);
        类名=   StringUtils.upperCaseFirst(实体名);
//        实体名 = StringUtils.lowerCaseFirst(类名);
    }

    String jpaLikeRows(){
        String rows="";
        for (ColumnInfo columnInfo : columnInfos) {
            String java字段名 = columnInfo.getJava字段名();
            String java字段名开头大写 = columnInfo.getJava字段名开头大写();
            String row="if (!StringUtils.isNone(#实体名#.getId())) {\n" +
                    "            list.add(criteriaBuilder.equal(root.get(\"#java字段名#\").as(String.class), #实体名#.get#java字段名开头大写#()));\n" +
                    "        }\n";
            row=  row.replace("#java字段名#",java字段名)
                    .replace("#java字段名开头大写#",java字段名开头大写)
                    .replace("#实体名#",实体名);
            rows+=row;
//            rows.replace("java字段名",java字段名).
        }
        return rows;


//        return rows;

    }

    public String  genController(String 包名){
        String code = "package #包名#.controller;\n" +
                "\n" +
                "import #包名#.entity.#类名#;\n" +
                "import #包名#.entity.Rotation;\n" +
                "import #包名#.repository.#类名#Repository;\n" +
                "import com.gm.wj.util.AddOrderSocket;\n" +
                "import com.gm.wj.util.JsonUtil;\n" +
                "import com.gm.wj.util.ReturnT;\n" +
                "import com.gm.wj.util.StrUtil;\n" +
                "import com.gm.wj.util.page.StarpPage;\n" +
                "import io.swagger.annotations.Api;\n" +
                "import io.swagger.annotations.ApiOperation;\n" +
                "import lombok.extern.slf4j.Slf4j;\n" +
                "import org.springframework.data.domain.*;\n" +
                "import org.springframework.data.jpa.domain.Specification;\n" +
                "import org.springframework.web.bind.annotation.*;\n" +
                "import org.springframework.beans.factory.annotation.Autowired;\n" +
                "\n" +
                "import java.util.*;\n" +
                "\n" +
                "import javax.persistence.criteria.Predicate;\n" +
                "\n" +
                "/**\n" +
                " * @author mqp\n" +
                " * @description #类名#\n" +
                " * @date 2022-06-27\n" +
                " */\n" +
                "\n" +
                "@Slf4j\n" +
                "@Api(tags = \"#实体名#\")\n" +
                "//@CrossOrigin\n" +
                "@CrossOrigin(allowCredentials = \"true\")\n" +
                "@RestController\n" +
                "//@RequestMapping(\"/#实体名#\")\n" +
                "@RequestMapping(\"/api/#实体名#\")\n" +
                "public class #类名#Controller {\n" +
                "\n" +
                "    @Autowired\n" +
                "    private #类名#Repository #实体名#Repository;\n" +
                "\n" +
                "    /**\n" +
                "     * 新增或编辑\n" +
                "     */\n" +
                "    @PostMapping(\"/save\")\n" +
                "    @ApiOperation(value = \"save #实体名#\", notes = \"save #实体名#\")\n" +
                "    public Object save(@RequestBody #类名# #实体名#) {\n" +
                "        try {\n" +
                "            return ReturnT.success(#实体名#Repository.save(#实体名#));\n" +
                "        } catch (Exception e) {\n" +
                "            e.printStackTrace();\n" +
                "            return ReturnT.error(\"保存失败\");\n" +
                "        }\n" +
                "\n" +
                "    }\n" +

                "    /**\n" +
                "     * 删除\n" +
                "     */\n" +
                "    @PostMapping(\"/delete\")\n" +
                "    @ApiOperation(value = \"delete #实体名#\", notes = \"delete #实体名#\")\n" +
                "    public Object delete(int id) {\n" +
                "        Optional<#类名#> #实体名# = #实体名#Repository.findById(id);\n" +
                "        if (#实体名#.isPresent()) {\n" +
                "            #实体名#Repository.deleteById(id);\n" +
                "            return ReturnT.success(\"删除成功\");\n" +
                "        } else {\n" +
                "            return ReturnT.error(\"没有找到该对象\");\n" +
                "        }\n" +
                "    }\n" +
                "\n" +
                "    /**\n" +
                "     * 查询\n" +
                "     */\n" +
                "    @PostMapping(\"/find\")\n" +
                "    @ApiOperation(value = \"find #实体名# by id\", notes = \"find #实体名# by id\")\n" +
                "    public Object find(int id) {\n" +
                "        Optional<#类名#> #实体名# = #实体名#Repository.findById(id);\n" +
                "        if (#实体名#.isPresent()) {\n" +
                "            return ReturnT.success(#实体名#.get());\n" +
                "        } else {\n" +
                "            return ReturnT.error(\"没有找到该对象\");\n" +
                "        }\n" +
                "    }\n" +
                "\n" +
                "    /**\n" +
                "     * 分页查询\n" +
                "     */\n" +
                "    @PostMapping(\"/list\")\n" +
                "    @ApiOperation(value = \"list #实体名#\", notes = \"list #实体名#\")\n" +
                "    public Object list(@RequestBody #类名# #实体名#,\n" +
                "                       @RequestParam(required = false, defaultValue = \"0\") int pageNumber,\n" +
                "                       @RequestParam(required = false, defaultValue = \"10\") int pageSize) {\n" +
                "\n" +
                "        try {\n" +
                "            //创建匹配器，需要查询条件请修改此处代码\n" +
                "            ExampleMatcher matcher = ExampleMatcher.matchingAll();\n" +
                "\n" +
                "            //创建实例\n" +
                "            Example<#类名#> example = Example.of(#实体名#, matcher);\n" +
                "            //分页构造\n" +
                "            Pageable pageable = PageRequest.of(pageNumber, pageSize);\n" +
                "\n" +
                "            return ReturnT.success(#实体名#Repository.findAll(example, pageable));\n" +
                "\n" +
                "        } catch (Exception e) {\n" +
                "            e.printStackTrace();\n" +
                "            return ReturnT.error(e.getMessage());\n" +
                "        }\n" +
                "\n" +
                "    }\n" +
                "\n" +
                "\n" +
                "    @PostMapping(\"/findAllById\")\n" +
                "    @ApiOperation(value = \"findAllById\", notes = \"findAllById\")\n" +
                "    public Object findAllById(@RequestBody List<Integer> ids,\n" +
                "                              @RequestParam(required = false, defaultValue = \"0\") int pageNumber,\n" +
                "                              @RequestParam(required = false, defaultValue = \"10\") int pageSize) {\n" +
                "        try {\n" +
                "            List<#类名#> allById =#实体名#Repository.findAllById(ids);\n" +
                "            StarpPage<#类名#> starpPage = new StarpPage<>(allById, pageNumber, pageSize);\n" +
                "            return ReturnT.success(starpPage);\n" +
                "        } catch (Exception e) {\n" +
                "            e.printStackTrace();\n" +
                "            return ReturnT.error(\"保存失败\");\n" +
                "        }\n" +
                "    }\n" +
                "\n" +
                "\n" +
                "    @PostMapping(\"/deleteInBatch\")\n" +
                "    @ApiOperation(value = \"deleteInBatch\", notes = \"deleteInBatch\")\n" +
                "    public Object deleteInBatch(@RequestBody List<#类名#> entities) {\n" +
                "        try {\n" +
                "            #实体名#Repository.deleteInBatch(entities);\n" +
                "            return ReturnT.success(\"批量删除成功\");\n" +
                "        } catch (Exception e) {\n" +
                "            e.printStackTrace();\n" +
                "            return ReturnT.error(\"批量删除失败\");\n" +
                "        }\n" +
                "    }\n" +
                "\n" +
                "    @PostMapping(\"/listLike\")\n" +
                "    @ApiOperation(value = \"listLike\", notes = \"listLike\")\n" +
                "    public Object listLike(@RequestBody #类名# #实体名#,\n" +
                "                           @RequestParam(required = false, defaultValue = \"0\") int pageNumber,\n" +
                "                           @RequestParam(required = false, defaultValue = \"10\") int pageSize) {\n" +
                "\n" +
                "        try {\n" +
                "\n" +
                "            Pageable pageable = PageRequest.of(pageNumber, pageSize);\n" +
                "            Page<#类名#> page = #实体名#Repository.findAll((Specification<#类名#>)\n" +
                "                    (root, criteriaQuery, criteriaBuilder) -> {\n" +
                "                        List<Predicate> list = new ArrayList<Predicate>();\n" +
                "  #jpaLikeRows# "+
                "\n" +
                "                        Predicate[] p = new Predicate[list.size()];\n" +
                "                        return criteriaBuilder.and(list.toArray(p));\n" +
                "                    }, pageable);\n" +
                "            return ReturnT.success(page);\n" +
                "\n" +
                "        } catch (Exception e) {\n" +
                "            e.printStackTrace();\n" +
                "            return ReturnT.error(e.getMessage());\n" +
                "        }\n" +
                "\n" +
                "    }\n" +
                "\n" +
                "}\n";

        String jpaLikeRows = jpaLikeRows();
        code=   code.replace("#jpaLikeRows#",jpaLikeRows)
                .replace("#实体名#",实体名)
                .replace("#类名#",类名)
                .replace("#包名#",包名);
        return code;

    }

    public String genFindFirstRows() {

        StringBuilder res = new StringBuilder();
        for (ColumnInfo columnInfo : columnInfos) {
//            String column_name = columnInfo.getCOLUMN_NAME();
            String java字段名 = columnInfo.getJava字段名();
//           ColumnInfo findFirstByCHARACTER_SET_NAME(String CHARACTER_SET_NAME);
//            column_name.ge
//            String 字段名开头大写 = StrUtil.captureName(column_name);
            String 字段名开头大写 = StringUtils.captureName(java字段名);
//            res+=类名+
//            String column_type = columnInfo.getCOLUMN_TYPE();
//            column_type
//            String data_type = columnInfo.getDATA_TYPE();
////            "varchar";
//            String java字段类型 = ColumnInfo.DATA_TYPE_Map.get(data_type);
//            if()
            String java字段类型 = columnInfo.获取java字段类型();
            res.append("#类名# findFirstBy#字段名开头大写#(#字段类型名字# #字段名#);\n"
                    .replace("#类名#", 类名)
                    .replace("#字段名开头大写#", 字段名开头大写)
//                    .replace("#字段名#", column_name))
                    .replace("#字段名#", java字段名)
                            .replace("#字段类型名字#", java字段类型)
            )

            ;
        }
        return res.toString();
    }

    public static void main(String[] args) {
        TableInfo tableInfo = new TableInfo();
//        tableInfo.se
        String genFindFirstRows = tableInfo.genFindFirstRows();
        System.out.println(genFindFirstRows);
    }
}
